//
//  SignInCountViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/12/20.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "SignInCountViewController.h"
#import "SignInCountCell.h"
#import <Masonry.h>
#import "JPUSHService.h"
#import "ZhujiaoNetwork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "SZLAccountViewController.h"
#import <MJRefresh.h>

@interface SignInCountViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *sources;
@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,weak) UILabel *sumTotalLab;

@end

@implementation SignInCountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"签到统计";
    [self setUpView];
    [self.view addSubview:self.tableView];
    [self getRegistrationList];
    self.curPage = 1;
    //极光获取自定义消息推送内容
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
}
- (void)networkDidReceiveMessage:(NSNotification *)notification
{
//    NSDictionary * userInfo = [notification userInfo];
//    NSString *content = [userInfo valueForKey:@"content"];
//    NSDictionary *extras = [userInfo valueForKey:@"extras"];
//    NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服务端传递的Extras附加字段，key是自己定义的
}
- (void)setUpView
{
    UIView *bottomView = [[UIView alloc]init];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 1)];
    lineView.backgroundColor = RGBColorOne(220);
    [bottomView addSubview:lineView];
    UILabel *moneyLab = [[UILabel alloc]initWithFrame:CGRectMake(20, 15, 150, 20)];
    moneyLab.text = @"收入总计: ¥0";
    moneyLab.font = FontB(16);
    moneyLab.textColor = [UIColor redColor];
    self.sumTotalLab = moneyLab;
    [bottomView addSubview:moneyLab];
    UIButton *cashBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-70, 0, 70, 50)];
    [cashBtn setTitle:@"提现" forState:UIControlStateNormal];
    [cashBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cashBtn.backgroundColor = [UIColor redColor];
    [cashBtn addTarget:self action:@selector(cashClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:cashBtn];
}
- (void)cashClick
{
    SZLAccountViewController *acountVc = [[SZLAccountViewController alloc]init];
    [self.navigationController pushViewController:acountVc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SignInCountCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SignInCountCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row];
    return cell;
}
- (void)getRegistrationList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"id"] = self.model.ID;
    __WEAK
    [self showClearBgWaiter];
    [ZhujiaoNetwork getRegistrationListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        [weakSelf.tableView.mj_header endRefreshing];
        if (success) {
            if (lists.count == 0) {
                [self showNoDataLab:self.tableView range:CGRectMake(0, (ScreenH-64-50)*0.5, ScreenW, 20) title:@"暂时没有人签到"];
            }else {
                [self.sources addObjectsFromArray:lists];
                [self.tableView reloadData];
                self.sumTotalLab.text = [NSString stringWithFormat:@"收入总计: ¥%.2f",[self calculateAllBackMoney]];
            }
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-SafeAreaTopHeight-50)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SignInCountCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([[SignInCountCell class] class])];
        _tableView.rowHeight = 90;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [[UIView alloc]init];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.curPage = 1;
            [self.sources removeAllObjects];
            [self getRegistrationList];
        }];
//        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            self.curPage++;
//        }];
    }
    return _tableView;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}
- (float)calculateAllBackMoney
{
    float total = 0;
    for (RegistrationListModel *model in self.sources) {
        total += [model.sumTotal floatValue];
    }
    return total;
}
@end
